package com.jy.adscm.zltx.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.jy.adscm.bean.system.JyMenu;
import com.jy.adscm.bean.system.JyUser;
import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.security.utils.Token;
import com.jy.adscm.support.SimpleCoreResponse;
import com.jy.adscm.utils.AdscmBaseUtils;
import com.jy.adscm.zltx.bean.SjFileTrain;
import com.jy.adscm.zltx.bean.SjFileUser;
import com.jy.adscm.zltx.service.SjFileTrainService;
import com.jy.adscm.zltx.service.SjFileUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 人员与三级文件配置器 前端控制器
 * </p>
 *
 * @author yyy
 * @since 2019-04-24
 */
@RestController
@RequestMapping("/adscm/zltx/sjFileUser")
public class SjFileUserController {

    @Autowired
    SjFileUserService sjFileUserService;
    @Autowired
    RedisTemplate<Object, Token> tokenRedisTemplate;
    /**
     * 查询三级文件树
     * @return
     * @throws AdscmSystemException
     */
    @RequestMapping("selectSjFileTree")
    public SimpleCoreResponse selectSjFileTree()throws AdscmSystemException {
        List<JyMenu> sjFileTree = sjFileUserService.selectSjFileTree();
        return new SimpleCoreResponse("200","查询成功",sjFileTree);
    }

    /**
     * 查询用户信息
     * @return
     * @throws AdscmSystemException
     */
    @RequestMapping("selectAllUsers")
    public SimpleCoreResponse selectAllUsers()throws AdscmSystemException {
        List<JyUser> jyUsers = sjFileUserService.selectAllUsers();
        return new SimpleCoreResponse("200","查询成功",jyUsers);
    }

    @RequestMapping("insert")
    public SimpleCoreResponse insert(@RequestBody List<SjFileUser> sjfile, HttpServletRequest request )throws AdscmSystemException {
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
         if (!AdscmBaseUtils.getInstance().isNotEmpty(token,null)){
            throw new AdscmSystemException("请重新登录");
         }
         //删除之前所有的这个人的关系
        Wrapper<SjFileUser> sjFileUserWrapper=new EntityWrapper<>();
         sjFileUserWrapper.eq("userId",token.getUser().getId());
        sjFileUserService.delete(sjFileUserWrapper);
        for (SjFileUser sjFileUser : sjfile) {
             if(!AdscmBaseUtils.getInstance().isNotEmpty(sjFileUser.getFileId(),null )){
                 continue;
             }
            sjFileUserService.insert(sjFileUser);
        }
        return new SimpleCoreResponse("200","查询成功",null);
    }
    @RequestMapping("selectSjFileTreeByUser/{id}")
    public SimpleCoreResponse selectSjFileTreeByUser(@PathVariable Integer id)throws AdscmSystemException {
        List<JyMenu> jyUsers = sjFileUserService.selectSjFileTreeByUser(id);
        return new SimpleCoreResponse("200","查询成功",jyUsers);
    }

    @RequestMapping("select")
    public SimpleCoreResponse select()throws AdscmSystemException {
        List<SjFileUser> SjFileUser = sjFileUserService.selectList(null);
        return new SimpleCoreResponse("200","查询成功",SjFileUser);
    }
}

